package com.zking.forward.view;

import com.zking.dao.ViewDao;
import com.zking.entity.User;
import com.zking.entity.View;
import com.zking.util.RedirectMsg;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

@WebServlet("/SubmitView")
public class SubmitView extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //处理请求的编码，解决中文乱码
        request.setCharacterEncoding("utf-8");
        //处理响应的编码 ，解决中文乱码
        response.setContentType("text/html;charset=utf-8");

        System.out.println("SubmitView===进来");
        //接收数据
        User user = (User)request.getSession().getAttribute("user");
        if(user==null){
            response.sendRedirect("ViewList");
            return;
        }

        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String time=now.format(formatter);
        String content=request.getParameter("guestContent");

        System.out.println(user);
        View view = new View(content,null, time, user.getEu_user_name(), user.getEu_user_id(), null);
        request.setAttribute("pageName", RedirectMsg.viewListPageName);
        request.setAttribute("url", RedirectMsg.viewListOfForwardUrl);
        boolean flag= ViewDao.addViews(view);
        if(flag)
        {
            request.setAttribute("msg", RedirectMsg.viewSuccess);
        }else{
            request.setAttribute("msg", RedirectMsg.viewError);
        }
        request.getRequestDispatcher("redirect.jsp").forward(request,response);



    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}